package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.ObserverManager;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.index.activity.IndexFileLogAddActivity;
import com.lepu.app.fun.index.bean.BeanIndexFileLog;
import com.lepu.pasm.R;

public class IndexFileLogAdapter extends BaseAdapter implements AsyncRequest{
	private String REQUEST_DELETE = "request_delete";
	
	private LayoutInflater layoutInflater = null;
	private Context context = null;
	private ArrayList<BeanIndexFileLog> knowList;
	
	private int deletePosition;
	
	public IndexFileLogAdapter(Context context , ArrayList<BeanIndexFileLog> knowList) {
		this.context = context;
		this.knowList = knowList;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		int size = 0;
		if(knowList != null && knowList.size() > 0)
		{
			size = knowList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.index_file_log_item, null);

			holder = new ViewHolder();
			TextView timeTextView = (TextView) convertView.findViewById(R.id.time_textview);
			TextView deleteTextView = (TextView) convertView.findViewById(R.id.delete_textview);
			TextView editTextView = (TextView) convertView.findViewById(R.id.edit_textview);
			TextView weightTextView = (TextView) convertView.findViewById(R.id.weight_textview);
			TextView heartTextView = (TextView) convertView.findViewById(R.id.heart_textview);
			TextView sbpTextView = (TextView) convertView.findViewById(R.id.sbp_textview);
			TextView dbpTextView = (TextView) convertView.findViewById(R.id.dbp_textview);
			TextView bloodTypeTextView = (TextView) convertView.findViewById(R.id.blood_type_textview);
			TextView bloodTextView = (TextView) convertView.findViewById(R.id.blood_textview);
			TextView noteTextView = (TextView) convertView.findViewById(R.id.note_textview);

			holder.timeTextView = timeTextView;
			holder.deleteTextView = deleteTextView;
			holder.editTextView = editTextView;
			holder.weightTextView = weightTextView;
			holder.heartTextView = heartTextView;
			holder.sbpTextView = sbpTextView;
			holder.dbpTextView = dbpTextView;
			holder.bloodTypeTextView = bloodTypeTextView;
			holder.bloodTextView = bloodTextView;
			holder.noteTextView = noteTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
        
		BeanIndexFileLog item = knowList.get(position);
		
		holder.timeTextView.setText(item.RecordCreateTime);
		if(!TextUtils.isEmpty(item.Weight)){
			holder.weightTextView.setText(item.Weight+"kg");
		}
		if(!TextUtils.isEmpty(item.HeartRate)){
			holder.heartTextView.setText(item.HeartRate+"次/分钟");
		}
		if(!TextUtils.isEmpty(item.SBP)){
			holder.sbpTextView.setText(item.SBP+"mmHg");
		}
		if(!TextUtils.isEmpty(item.DBP)){
			holder.dbpTextView.setText(item.DBP+"mmHg");
		}
		if(!TextUtils.isEmpty(item.BloodGlucose)){
			holder.bloodTextView.setText(item.BloodGlucose+"mmol/L");
		}
		
		String[] mealArray = context.getResources().getStringArray(R.array.blood_select_test_time_array);
		holder.bloodTypeTextView.setText(mealArray[item.BloodGlucoseType-1]+"血糖:");
		
		holder.noteTextView.setText(item.Description);

		holder.deleteTextView.setOnClickListener(new ViewOnclick(position));
		holder.editTextView.setOnClickListener(new ViewOnclick(position));
		
		return convertView;
	}
	
	class ViewOnclick implements View.OnClickListener{
		private int position;
		public ViewOnclick(int position){
			this.position = position;
			deletePosition = position; //记录delete下标
		}

		@Override
		public void onClick(View v) {
			 switch (v.getId()) {
				case R.id.delete_textview:
					showDeleteItem(knowList.get(position));
					break;
				case R.id.edit_textview:
					Intent intent = new Intent((BaseFragmentActivity)context, IndexFileLogAddActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("fileLog", knowList.get(position));
					intent.putExtras(bundle);
					((BaseFragmentActivity)context).startActivity(intent,true);
					break;
				default:
					break;
			 }
		}
		
	}
	
	private void showDeleteItem(final BeanIndexFileLog beanFileLog) {
		new MyDialog((BaseFragmentActivity) context)
				.setMessage("确定删除数据?")
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								
							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						deleteItem(beanFileLog);
					}
				}).create(null).show();

	}
	
	public void deleteItem(BeanIndexFileLog beanFileLog) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			
			params.put("HRID", beanFileLog.HRID);

		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post(Setting.getIndexFileLogDelete(), map, null, this,REQUEST_DELETE, true, true);
		((BaseFragmentActivity)context).showProgressDialog();
	}
	
	static class ViewHolder {
		TextView timeTextView;
		TextView deleteTextView;
		TextView editTextView;
		TextView weightTextView;
		TextView heartTextView;
		TextView sbpTextView;
		TextView dbpTextView;
		TextView bloodTypeTextView;
		TextView bloodTextView;
		TextView noteTextView;
	}

	@Override
	public void RequestComplete(Object arg0, Object arg1) {
		((BaseFragmentActivity)context).hideProgressDialog();
		//通知listview刷新
		ObserverManager.getInstance().notify(ObserverConst.NOTIFY_FILE_LOG_CHANGE, null, deletePosition);
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		((BaseFragmentActivity)context).hideProgressDialog();
	}

}
